package com.sky.service.user.impl;


import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.mapper.user.DishFlavorMapper;
import com.sky.mapper.user.UDishMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.service.user.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service("userDishService")
public class DishServiceImpl implements DishService {
    @Autowired
    private UDishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public List<DishVO> findDishByCategory(Dish dish) {
        /*
          传进来的dish对象只包含categoryId和status两个属性
          根据该对象传参给mapper层查询所有符合条件的dish对象,得到对象集合
          同时,遍历该集合,取出每一个dish的id,传给DishFlavorMapper层,查询所有的口味信息,封装成集合
          把该集合赋给dishVO对象,最后将dishVO封装成集合返回给前端
         */
        List<Dish> dishList = dishMapper.findDishByCategory(dish);
        if(dishList==null || dishList.size()==0){
            return null;
        }
        //创建DishVO对象集合
        List<DishVO> dishVOList = new ArrayList<>();
        //遍历dishList集合,通过工具复制给DishVO
        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
